* ======================================
* ------- Summary municipalities -------
* ======================================

	
* Source from own data
* ====================
	
	* Compute immigrant share and vote-eligible population
	use $dta\Population.dta, clear
	keep if year == 2015
	merge 1:1 idPers year using "$dta\ImportGeo_raw.dta", nogen ///
		keep(matched) // Use raw b/c harmonized version omits several knrs
	gen popvote = 1
	rename immigrant immshare
	collapse (mean) immshare (sum) popvote, by(knr)
	
	tempfile sampledata
	save `sampledata'
	
	
* Source from FHN
* ===============

	* Import municipality level data from FHN (2020)
	use "$dta_ext\FivaHalseNatvik2020.dta", clear
	keep year pop knr kname children young elderly women unemployment Turnout
	keep if year == 2015 | year == 2016
	bysort knr: replace Turnout = Turnout[2] // 2015 turnout coded as 2016
	rename Turnout turnout
	keep if year == 2015
	
	* Convert to string
	gen knr_str = strofreal(knr)
	replace knr_str = "0" + knr_str if strlen(knr_str) == 3
	drop knr
	rename knr_str knr
	replace knr = "5001" if knr == "1601" // Trondheim
	
	
* Combine sources
* ===============

	* immshare and votepop
	merge 1:1 knr using `sampledata', nogen keep(master matched)
		
	* Import knr's with complete turnout data
	merge 1:1 knr using "$dta\Turnout_knr.dta", nogen keep(master matched)
	replace knr_inSample = 0 if knr_inSample != 1
	

	
* Summarize
* =========

	*Percentize
	foreach var of varlist children young elderly women unemployment immshare turnout{	
		gen `var'100 = `var'*100
	}


	forvalues i = 0/1{
		preserve
		keep if knr_inSample == `i'
		
		* Population
		sum pop // population
		local m_pop_`i' = string(r(mean),"%15.0fc")
		local sd_pop_`i' = string(r(sd), "%15.0fc")
		local obs`i' = string(r(N), "%15.0fc")
		sum popvote // vote-eligible population
		local m_popvote_`i' = string(r(mean),"%15.0fc")
		local sd_popvote_`i' = string(r(sd), "%15.0fc")
		foreach var of varlist *100{ // summary variable
			sum `var'
			local m_`var'_`i' = string(r(mean),"%15.2fc")
			local sd_`var'_`i' = string(r(sd), "%15.2fc")
		}
		
		restore
	}

	
* Write table
* ===========

	capture file close summary
	file open summary using "$tbls\SummaryMunicipalities.tex", write replace
	file write summary "\begin{tabular}{lcccc} \hline\hline" _n
	file write summary " & \multicolumn{2}{c}{\shortstack{\\ \\ Included \\ municipalities}} & \multicolumn{2}{c}{\shortstack{\\ \\ Excluded \\ municipalities}}\\ \cmidrule(lr){2-3} \cmidrule(lr){4-5}"_n
	file write summary " & Mean & SD  & Mean & SD \\ \hline" _n
	file write summary "  &   &  &  &  \\  " _n
	file write summary "Population & `m_pop_1'  & `sd_pop_1' & `m_pop_0' & `sd_pop_0'  \\" _n
	file write summary "Vote-eligible population & `m_popvote_1'  & `sd_popvote_1' & `m_popvote_0' & `sd_popvote_0'  \\" _n
	file write summary "Pre-school age (percent) & `m_children100_1'  & `sd_children100_1' & `m_children100_0' & `sd_children100_0'  \\" _n
	file write summary "School age (percent) & `m_young100_1'  & `sd_young100_1' & `m_young100_0' & `sd_young100_0'  \\" _n
	file write summary "66 years and older (percent) & `m_elderly100_1'  & `sd_elderly100_1' & `m_elderly100_0' & `sd_elderly100_0'  \\" _n
	file write summary "Women (percent) & `m_women100_1'  & `sd_women100_1' & `m_women100_0' & `sd_women100_0'  \\" _n
	file write summary "Unemployed (percent) & `m_unemployment100_1'  & `sd_unemployment100_1' & `m_unemployment100_0' & `sd_unemployment100_0'  \\" _n
	file write summary "Immigrants (percent) & `m_immshare100_1'  & `sd_immshare100_1' & `m_immshare100_0' &  `sd_immshare100_0'  \\" _n
	file write summary "Turnout (percent) & `m_turnout100_1'  & `sd_turnout100_1' & `m_turnout100_0' & `sd_turnout100_0'  \\ \hline " _n
	file write summary "N &  `obs1' &  & `obs0' &  \\ " _n
	file write summary "\hline \hline" _n
	file write summary "\end{tabular}"
	file close summary